package it.polito.connectedcar.message;

import it.polito.connectedcar.tcc.zone.Token;
import it.polito.connectedcar.util.Constants;

/**
 * This message is sent by a vehicle to a TCC to inform that a new token has
 * been assigned to the named vehicle. the content of the message is composed by
 * the token to leave and the new token being accepted
 * 
 * @author Nicola Aresta
 * 
 */
public class GoodbyeMessage extends OutgoingMessage {

	private Token goodbyeToken = null;
	private Token vehicleIdentifier;

	public GoodbyeMessage(Token t) {
		super(Constants.VEHICLE_TYPE_CODE,Constants.VEHICLE_GOODBYE_MESSAGE_CODE);
		this.goodbyeToken = t;
	}

	private GoodbyeMessage() {
		super(Constants.VEHICLE_TYPE_CODE,Constants.VEHICLE_GOODBYE_MESSAGE_CODE);
	};

	@Override
	protected void convertToData(Storage st, boolean forTCC, Token t) {
		// write the old token
		for (int i = 0; i < goodbyeToken.getBytes().length; i++)
			st.writeByte(goodbyeToken.getBytes()[i]);

		// write the new token
		for (int i = 0; i < t.getBytes().length; i++)
			st.writeByte(t.getBytes()[i]);

		st.writeStringASCII(vehicleIdentifier.toString());
	}

	public void setBelongingToken(Token zoneToken) {
		// TODO not implemented
	}

	public Token getVehicleIdentifier() {
		return vehicleIdentifier;
	}

	public void setVehicleIdentifier(Token vehicleID) {
		this.vehicleIdentifier = vehicleID;
	}
}
